﻿Imports System.Data.OleDb

Public Class Home
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Dim TagCountSql As String = "Select Tag_fkid, COUNT (*) AS TagCount FROM VideoTagJunction GROUP BY Tag_fkid"
        Dim TagCountSql As String = "SELECT COUNT (*) AS TagCount, Tags.[Tags], VideoTagJunction.[Tag_fkid]  FROM Tags INNER JOIN VideoTagJunction ON Tags.[ID] = VideoTagJunction.[Tag_fkid] GROUP BY VideoTagJunction.[Tag_fkid], Tags.[Tags] ORDER BY Tags.[Tags]"
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("3dBuzzDatabase").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(TagCountSql, oleDbConn)
        oleDbConn.Open()
        cmd.CommandType = CommandType.Text
        Dim TagCountReader = cmd.ExecuteReader()
        TagCountReader.Read()
        
        'first time through the loop we find out the highest and lowest
        Dim tagHash As New Hashtable  'will contain key value pairs, keys must be distinct - can not have the same key twice
        Dim MaxTagCount As Integer = 0
        Dim MinTagCount As Integer = 0
        If TagCountReader.HasRows() Then
            While TagCountReader.Read()
                ' Response.Write(" " & TagCountReader.Item("Tag_fkid") & " " & TagCountReader.Item("Tags") & " " & TagCountReader.Item("TagCount"))  'TagCount is the alias I created to contain the count of each tag
                tagHash.Add(TagCountReader.Item("Tags"), CInt(TagCountReader.Item("TagCount")))
                'what is the min and max number
                If MinTagCount = 0 Then MinTagCount = CInt(TagCountReader.Item("TagCount")) '1st setting of variables
                If MaxTagCount = 0 Then MaxTagCount = CInt(TagCountReader.Item("TagCount"))

                If MinTagCount > CInt(TagCountReader.Item("TagCount")) Then     'find min number of occurrencies
                    MinTagCount = CInt(TagCountReader.Item("TagCount"))
                End If


                If MaxTagCount < CInt(TagCountReader.Item("TagCount")) Then      'find max number of occurrencies
                    MaxTagCount = CInt(TagCountReader.Item("TagCount"))
                End If

            End While
        End If

        'establish an array of font sizes
        Dim fontOptions() As String = {"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large"}  'css classification of font size
        'associate a number of occurrence with 1 of the 7 font sizes
        'create a unit of scale
        Dim scaleUnit As Decimal = (MaxTagCount - MinTagCount + 1) / Convert.ToDecimal(fontOptions.Length)

        'html variable
        Dim tagCloudHtml As String = " "
        Dim storeTag As Integer = 0
        Dim storeTaga As String = ""
        Dim videoPage As String = "TagCloudSelection.aspx?"

        For Each tmpTag In tagHash


            'find the id which can not be stored in the hash table as it only stores pairs
            Dim findTagIdSql As String = "SELECT Tags.ID FROM Tags WHERE Tags.Tags = @f1"
            Dim findTagcmd As OleDbCommand = New OleDbCommand(findTagIdSql, oleDbConn)
            findTagcmd.CommandType = CommandType.Text
            findTagcmd.Parameters.AddWithValue("@f1", tmpTag.key)

            Dim dataReader = findTagcmd.ExecuteReader()
            dataReader.Read()
            storeTag = dataReader.Item("ID")
            storeTaga = storeTag.ToString

            'scale value will be 1 - 7 representing font size
            Dim scaleValue As Integer = Math.Truncate(tmpTag.value - MinTagCount / scaleUnit)
            If scaleValue < 1 Then   'cleared out db and tested it empty - got an out of bounds error, this stops that - phew
                scaleValue = 1
            End If
            tagCloudHtml = tagCloudHtml & String.Format("<A HREF = ""{0}"" style = ""font-size:{1};""> {2} </A> &nbsp;&nbsp;", videoPage & "ID=" & storeTaga, fontOptions(scaleValue), tmpTag.key, storeTaga)

        Next
        phTagContainer.Controls.Add(New LiteralControl(tagCloudHtml))

        '2nd time we go through the loop stored in the hash table we can display them with the right size

    End Sub

End Class